package com.kehongborui.empmanagesys.dao;

import com.kehongborui.empmanagesys.entity.User;
import com.kehongborui.empmanagesys.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief 用户dao
 * @email 122162005@qq.com
 * @date 2018-07-18
 */
@Deprecated
public class UserDao {
    /**
     * 根据用户名查找用户是否存在
     * @param username
     * @return
     */
    public User findByUsername(String username) throws SQLException {
        // 获取数据库连接
        Connection connection = DbUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement("select  * from  khbr_user where username=?");
        statement.setString(1, username);
        // 获取结果集并解析
        ResultSet rs = statement.executeQuery();

        User user = null;
        if (rs.next()) {
            user = new User();
            user.setAge(rs.getInt("age"));
            user.setGender(rs.getInt("gender"));
            user.setPassword(rs.getString("password"));
            user.setUsername(rs.getString("username"));
            user.setRealName(rs.getString("real_name"));
        }

        // 关闭结果集
        DbUtil.closeRsStmt(rs, statement);
        DbUtil.closeConnection(connection);
        return user;
    }
}
